/*
 * Copyright (c) 2018. czmc All Rights Reserved.
 * Author: czmc
 * Email: zxc6355533@163.com
 * Encoding: UTF-8
 */

package top.stime21.myneighbours.module.me

import android.os.Bundle
import android.text.TextUtils
import android.view.View
import kotlinx.android.synthetic.main.activity_edit_name.*
import kotlinx.android.synthetic.main.include_header.*
import top.stime21.myneighbours.R
import top.stime21.myneighbours.api.ApiManager
import top.stime21.myneighbours.base.BuzBaseActivity
import top.stime21.myneighbours.base.io_main
import top.stime21.myneighbours.data.entity.User
import top.stime21.myneighbours.utils.ACache

class EditNameActivity : BuzBaseActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_edit_name)
        initData()
    }

    private lateinit var user: User

    private fun initData() {
        tv_title.setText("编辑昵称")
        btn_back.setOnClickListener { finish() }
        user = ACache.get(this).getAsObject("user") as User
        et_nickname.setText(user.nickName)
        btn_right.visibility = View.VISIBLE
        btn_right.setOnClickListener({
            if (TextUtils.isEmpty(et_nickname.text.toString())) {
                showToast("昵称不能为空")
                return@setOnClickListener
            }
            showLoadding()
            ApiManager.ins().dataApi.updateInfo("nickName", et_nickname.text.toString(), user.userId).io_main()
                    .subscribe({ result ->
                        if (result.data != null) {
                            ACache.get(this@EditNameActivity).put("user", user)
                            finish()
                        }
                        hideLodding()
                    }, { t ->
                        hideLodding()
                        showToast("更改失败")
                    })
        })
        btn_delete.setOnClickListener(
                {
                    val text = et_nickname.text.toString()
                    if (text.length > 0) {
                        et_nickname.setText("")
                    }
                })
    }
}
